<?php


namespace App\Shuixing\Controllers;

use App\Admin\Repositories\WxMenu;
use App\Lib\WeApp\WeApp;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\Settings;
use App\Http\Controllers\Api\Repositories\WxTagsRepositories;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Models\Circle\WxCircle;
use App\Models\User\WxUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

class IndexController
{
    public function index()
    {
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;

        $pc_mdoe = Settings::get('pc_mdoe', 0);
        if($pc_mdoe != 0){

        }

        $qrcode_list = Settings::get('pc_index_qrcode_list', ['app', 'mini']);
        //社区贡献者
        $userdata = WxUser::whereIn('id', explode(',', Settings::get('pc_index_contributors', '')))
            ->get(['id', 'user_name', 'user_avatar', 'user_introduce', 'is_member', 'is_official', 'is_authentication']);

        //推荐圈子
        $tcdata = WxCircle::where('circle_state', 1)
            ->where('is_top_recommend', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'desc')->limit(3)->get();
        //最火圈子
        $hcdata = WxCircle::where('circle_state', 1)
            ->where('is_hot', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('sort', 'desc')->limit(3)->get();
        //最新圈子
        $xcdata = WxCircle::where('circle_state', 1)
            ->withCount('wxPosts as posts_count')
            ->withCount('wxUserCircle as user_circle_count')
            ->orderBy('id', 'desc')->limit(3)->get();

        //轮播笔记
        $lpdata = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->where('is_information', 1)
            ->orderBy('id', 'desc')->get();
        $lpdata = self::postsParame($lpdata);//文章详情

        //4篇推荐笔记
        $tpdata = WxPost::where('is_examine', 1)
            ->where('posts_state', 0)
            ->orderBy('id', 'desc')->limit(4)->get();
        $tpdata = self::postsParame($tpdata);//文章详情

        $hottag = WxTagsRepositories::hot_tags(10004);

        return view('shuixing/index', ['qrcodes' => $qrcode_list, 'tpdata' => $tpdata, 'lpdata' => $lpdata, 'userdata' => $userdata, 'tcdata' => $tcdata, 'hcdata' => $hcdata, 'xcdata' => $xcdata, 'hottag'=>$hottag]);
    }

    public function wxLogin(Request $request)
    {
        _logger_(__file__, __line__, $request->scene . '-----' .$request->token);
        if(!_empty_($request->scene) && !_empty_($request->token)){
            $res = Cache::put('login:scene:'.$request->scene, $request->token, 60);
        }
        return true;
    }

    public function wxLoginCode(){
        //例如，在laravel,文件缓存路径为storage_path('cache/')
        $weapp = new WeApp();
        //从‘小程序’获取一个‘二维码’单例对象
        $qrcode = $weapp->getQRCode();
        $scene = md5(uniqid());
//        $page = 'pages/login/login';
        $page = 'pages/pc-login/index';
        //2.获取小程序B码
        $qrcodeContent = $qrcode->getQRCodeB($scene, $page, 430, false, $line_color = null);
        $filePath = 'qrcode/' . md5(uniqid()) . '.png';
        $res = Storage::disk('public')->put($filePath, $qrcodeContent);
        if ($res) {
            return Storage::url($filePath);
        }else{
            return '';
        }
    }

    public function pcLogin(Request $request)
    {
        $token = Cache::get('login:scene:'.$request->scene);
        if($token){
            $data = [
                'token' => $token,
                'scene' => $request->scene
            ];
            return $data;
        }else{
            return null;
        }

    }

    public static function postsParame($data)
    {
        $data->map(function ($v) {
            $v->user = WxUser::where('id', $v->user_id)
                ->first(FieldUtils::userInfoColums());//发帖人
            $tagsIds = WxPostsTag::where('posts_id', $v->id)->pluck('tags_id');//话题ID列表
            $v->tags = WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//话题列表
            $v->like_count = WxLike::where('posts_id', $v->id)->count(); // 笔记喜欢数量
            $v->comment_count = WxComment::where([['posts_id', '=', $v->id], ['comment_state', '=', 1]])->count(); // 评论数量
            $v->image = WxPostsImg::where('posts_id', $v->id)->value('img_url'); // 笔记图片
            $v->vimage = WxPostsVideo::where('posts_id', $v->id)->value('video_thumb_url'); // 笔记视频封面
            $v->format_time = format_datetime($v->created_at); // 发帖时间
        });

        return $data;
    }


}